package com.v2future.sjt.collegecheckingattendance.fruit.research;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.GridView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.event.adapter.ReleaseEventImgGridAdapter;
import com.v2future.sjt.collegecheckingattendance.event.bean.ReleaseEventImageBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ResearchTypeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.research.fragment.ArticleInputFragment;
import com.v2future.sjt.collegecheckingattendance.fruit.research.fragment.OtherInputFragment;
import com.v2future.sjt.collegecheckingattendance.fruit.research.fragment.PatentInputFragment;
import com.v2future.sjt.collegecheckingattendance.fruit.research.fragment.ProjectInputFragment;
import com.v2future.sjt.collegecheckingattendance.fruit.research.fragment.TreatiseInputFragment;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by minnie on 16/5/23.
 */
public class ResearchDetailActivity extends MainActivity {

    ConditionBean conditionBean;
    ResearchTypeBean researchTypeBean;
    @Bind(R.id.layout_input)
    FrameLayout layoutInput;

    ArticleInputFragment articleInputFragment;
    TreatiseInputFragment treatiseInputFragment;
    ProjectInputFragment projectInputFragment;
    PatentInputFragment patentInputFragment;
    OtherInputFragment otherInputFragment;

    /**
     * 适配器装载图片的列表
     */
    List<ReleaseEventImageBean> imageDataList;

    /**
     * 图片展示适配器
     */
    ReleaseEventImgGridAdapter adapter;
    @Bind(R.id.gv_images)
    GridView gvImages;

    @Override
    public void init() {
        conditionBean = (ConditionBean) getIntent().getSerializableExtra(IntentKey.CONDITION);
        researchTypeBean = (ResearchTypeBean) getIntent().getSerializableExtra(IntentKey.RESEARCH_TYPE);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_research_detail_new);
        ButterKnife.bind(this);
        initTitleBack(null);
        setTitleText(researchTypeBean.getResearchSubjectName());
    }

    @Override
    public void initAfter() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentKey.CONDITION, conditionBean);
        articleInputFragment = new ArticleInputFragment();
        articleInputFragment.setArguments(bundle);
        treatiseInputFragment = new TreatiseInputFragment();
        treatiseInputFragment.setArguments(bundle);
        projectInputFragment = new ProjectInputFragment();
        projectInputFragment.setArguments(bundle);
        patentInputFragment = new PatentInputFragment();
        patentInputFragment.setArguments(bundle);
        otherInputFragment = new OtherInputFragment();
        otherInputFragment.setArguments(bundle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if ("1".equals(researchTypeBean.getResearchSubjectId())) {//论文
            transaction.replace(R.id.layout_input, articleInputFragment);
        } else if ("2".equals(researchTypeBean.getResearchSubjectId())) {//专著
            transaction.replace(R.id.layout_input, treatiseInputFragment);
        } else if ("3".equals(researchTypeBean.getResearchSubjectId())) {//项目
            transaction.replace(R.id.layout_input, projectInputFragment);
        } else if ("4".equals(researchTypeBean.getResearchSubjectId())) {//专利
            transaction.replace(R.id.layout_input, patentInputFragment);
        } else if ("5".equals(researchTypeBean.getResearchSubjectId())) {//其他
            transaction.replace(R.id.layout_input, otherInputFragment);
        }
        transaction.commitAllowingStateLoss();
        if (null != conditionBean.getImageUrl() && conditionBean.getImageUrl().size() > 0) {
            imageDataList = new ArrayList<>();
            for (int i = 0; i < conditionBean.getImageUrl().size(); i++) {
                ReleaseEventImageBean bean = new ReleaseEventImageBean();
                bean.setImageUrl(conditionBean.getImageUrl().get(i));
                imageDataList.add(bean);
            }
            adapter = new ReleaseEventImgGridAdapter(this, imageDataList);
            gvImages.setAdapter(adapter);
        }
    }

}
